/*
 * @Author: monster.huangwei 
 * @Date: 2019-01-23 18:00:13 
 */
import {Tools} from '../lib/tools'
import toast from '../lib/toast'

function Validate() {
  this.cache = []
  this.start = (rules) => {
    first:
    for (var i = 0; i < rules.length; i++) {
      for (var j = 0; j < (rules[i].rule).length; j++) {
        let str = (rules[i].rule)
        let ruleChild, parmas, flag
        if ((str[j].name).indexOf(':') > -1) {
          ruleChild = (str[j].name).split(':')[0]
          parmas = (str[j].name).split(':')[1]
          flag = (Tools.check)[ruleChild](rules[i].val, parmas)
        } else {
          flag = (Tools.check)[str[j].name](rules[i].val)
        }
        if (!flag) {
          this.cache.push(str[j].errMsg)
          toast(str[j].errMsg)
          break first
        }
      }
    }
  }
  this.end = (func) => {
    if (Tools.check.isEmpty(this.cache)) return false;
    func()
  }
}

export default Validate